﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.MEP.Electric.Common;
using HYRElectric.WirePlace;

namespace HYRElectric.Equipment
{
	public partial class FireRectLayoutForm : System.Windows.Forms.Form
	{
		public FireRectLayoutForm(Document document, FireDetectorType type)
		{
			this.InitializeComponent();
			this.doc = document;
			this.detectorType = type;
		}

		private void FireRectLayoutForm_Load(object sender, EventArgs e)
		{
			this.InitForm();
			this.ReadFormDataFromXml();
			if (this.detectorType == FireDetectorType.Detector_Smoke)
			{
				this.SetRadius("5.8");
				return;
			}
			this.SetRadius("3.6");
		}

		private void InitForm()
		{
			//base.Icon = Resource.YArchitechIcon;
			this.txtMaxSpace.MostValue = new MostValue(0.01, 100.0);
			this.InitRoofSlopeCombox();
			this.InitWireStyleCombox();
		}

		private void InitRoofSlopeCombox()
		{
			this.cmbRoofSlope.Items.Clear();
			this.cmbRoofSlope.Items.Add("θ≤15°");
			this.cmbRoofSlope.Items.Add("15°＜θ≤30°");
			this.cmbRoofSlope.Items.Add("θ＞30°");
			this.cmbRoofSlope.SelectedIndex = 0;
		}

		private void InitWireStyleCombox()
		{
			this.cmbWireStyle.Items.Add("弧线");
			this.cmbWireStyle.Items.Add("倒角");
			this.cmbWireStyle.SelectedIndex = 1;
		}

		private void UpdateRadiusCombox()
		{
			this.cmbRadius.Items.Clear();
			int selectedIndex = this.cmbRoofSlope.SelectedIndex;
			if (this.detectorType == FireDetectorType.Detector_Smoke)
			{
				if (selectedIndex != 1)
				{
					if (selectedIndex == 2)
					{
						this.cmbRadius.Items.Add("8.0");
						this.cmbRadius.Items.Add("9.0");
						this.cmbRadius.Items.Add("9.9");
					}
					else
					{
						this.cmbRadius.Items.Add("5.8");
						this.cmbRadius.Items.Add("6.7");
					}
				}
				else
				{
					this.cmbRadius.Items.Add("7.2");
					this.cmbRadius.Items.Add("8.0");
				}
			}
			else if (selectedIndex != 1)
			{
				if (selectedIndex == 2)
				{
					this.cmbRadius.Items.Add("5.5");
					this.cmbRadius.Items.Add("6.3");
				}
				else
				{
					this.cmbRadius.Items.Add("3.6");
					this.cmbRadius.Items.Add("4.4");
				}
			}
			else
			{
				this.cmbRadius.Items.Add("4.9");
			}
			if (this.cmbRadius.Items.Count > 0)
			{
				this.cmbRadius.SelectedIndex = 0;
			}
		}

		private void SelectRoofSlope(int index)
		{
			if (this.cmbRoofSlope.Items.Count <= 0)
			{
				return;
			}
			if (index < 0 || index >= this.cmbRoofSlope.Items.Count)
			{
				index = 0;
			}
			this.cmbRoofSlope.SelectedIndex = index;
		}

		private void SetRadius(string value)
		{
			if (!Assist.StringUtil.isNumberic2(value))
			{
				if (this.cmbRadius.Items.Count > 0)
				{
					this.cmbRadius.SelectedIndex = 0;
					return;
				}
				this.cmbRadius.Text = "0";
				return;
			}
			else
			{
				if (Convert.ToDouble(value) > 0.0)
				{
					this.cmbRadius.Text = value;
					return;
				}
				if (this.cmbRadius.Items.Count > 0)
				{
					this.cmbRadius.SelectedIndex = 0;
					return;
				}
				this.cmbRadius.Text = "0";
				return;
			}
		}

		private void UpdateMaxSpaceTextboxState()
		{
			this.txtMaxSpace.Enabled = this.checkManual.Checked;
		}

		private void UpdateMaxSpaceTextbox()
		{
			if (!this.txtMaxSpace.Enabled)
			{
				double value = this.CalMaxSpace();
				this.txtMaxSpace.Text = Math.Round(value, 2).ToString();
			}
		}

		private void UpdateWireControl()
		{
			this.buttonWire.Enabled = !this.radioNoneLine.Checked;
			this.cmbWireStyle.Enabled = !this.radioNoneLine.Checked;
		}

		private void SelectWireStyle(int index)
		{
			if (this.cmbWireStyle.Items.Count <= 0)
			{
				return;
			}
			if (index < 0 || index >= this.cmbWireStyle.Items.Count)
			{
				index = 0;
			}
			this.cmbWireStyle.SelectedIndex = index;
		}

		private double CalMaxSpace()
		{
			string text = this.cmbRadius.Text;
			if (!Assist.StringUtil.isNumberic2(text))
			{
				return 0.0;
			}
			double num = Convert.ToDouble(text);
			return 2.0 * num * Math.Cos(AssistFunc.AngleToRadian(45.0));
		}

		private void ReadFormDataFromXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\FireRect";
			xmlHelper.SelectNode(path);
			int num = Convert.ToInt32(xmlHelper.GetAttributeEx("RoofSlope", "0"));
			this.SelectRoofSlope(num);
			string attributeEx = xmlHelper.GetAttributeEx("Radius", "0");
			bool @checked = Convert.ToBoolean(xmlHelper.GetAttributeEx("IsManual", "false"));
			this.checkManual.Checked = @checked;
			this.UpdateMaxSpaceTextboxState();
			if (this.checkManual.Checked)
			{
				attributeEx = xmlHelper.GetAttributeEx("MaxSpace", "8");
				this.txtMaxSpace.Text = decimal.Round(decimal.Parse(attributeEx), 2).ToString();
			}
			else
			{
				this.UpdateMaxSpaceTextbox();
			}
			attributeEx = xmlHelper.GetAttributeEx("Level", "3");
			this.txtLevel.Text = attributeEx;
			num = Convert.ToInt32(xmlHelper.GetAttributeEx("LinkWay", "0"));
			if (num == 0)
			{
				this.radioNoneLine.Checked = true;
			}
			else if (num == 1)
			{
				this.radioRowLine.Checked = true;
			}
			else
			{
				this.radioColLine.Checked = true;
			}
			this.UpdateWireControl();
			num = Convert.ToInt32(xmlHelper.GetAttributeEx("WireStyle", "1"));
			this.SelectWireStyle(num);
			@checked = Convert.ToBoolean(xmlHelper.GetAttributeEx("IsPreview", "true"));
			this.checkPreview.Checked = @checked;
			int x = Convert.ToInt32(xmlHelper.GetAttributeEx("X", "100"));
			int y = Convert.ToInt32(xmlHelper.GetAttributeEx("Y", "100"));
			base.Location = new System.Drawing.Point(x, y);
		}

		private void SaveFormDataToXml()
		{
			YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper(Path.Combine(Product.UserDataLocation, "ElectricEquipment.xml"), "ElectricData");
			string path = "LayoutWas\\FireRect";
			xmlHelper.SelectNode(path);
			xmlHelper.SetAttribute("RoofSlope", this.cmbRoofSlope.SelectedIndex.ToString());
			xmlHelper.SetAttribute("Radius", this.Radius.ToString());
			xmlHelper.SetAttribute("IsManual", this.checkManual.Checked.ToString());
			xmlHelper.SetAttribute("MaxSpace", this.MaxSpace.ToString());
			xmlHelper.SetAttribute("Level", this.Level.ToString());
			xmlHelper.SetAttribute("LinkWay", this.LinkLineWay.ToString());
			xmlHelper.SetAttribute("WireStyle", this.WireStyle.ToString());
			xmlHelper.SetAttribute("IsPreview", this.IsPreview.ToString());
			xmlHelper.SetAttribute("X", base.Location.X.ToString());
			xmlHelper.SetAttribute("Y", base.Location.Y.ToString());
			xmlHelper.SaveXml("");
		}

		private void cmbRoofSlope_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.UpdateRadiusCombox();
		}

		private void cmbRadius_SelectedValueChanged(object sender, EventArgs e)
		{
			this.UpdateMaxSpaceTextbox();
		}

		private void cmbRadius_TextChanged(object sender, EventArgs e)
		{
			this.UpdateMaxSpaceTextbox();
		}

		private void cmbRadius_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!StringTools.IsNumber(e.KeyChar))
			{
				e.Handled = true;
			}
			else if (string.IsNullOrEmpty(this.cmbRadius.Text.Trim()) && '.' == e.KeyChar)
			{
				e.Handled = true;
			}
			base.OnKeyPress(e);
		}

		private void cmbRadius_Leave(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cmbRadius.Text.Trim()))
			{
				if (this.cmbRadius.Items.Count > 0)
				{
					this.cmbRadius.SelectedIndex = 0;
					return;
				}
				this.cmbRadius.Text = "0";
			}
		}

		private void checkManual_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateMaxSpaceTextboxState();
			this.UpdateMaxSpaceTextbox();
		}

		private void radioNoneLine_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateWireControl();
		}

		private void buttonWire_Click(object sender, EventArgs e)
		{
			new NewWireParamForm(this.doc).ShowDialog();
		}

		private void FireRectLayoutForm_Deactivate(object sender, EventArgs e)
		{
			this.cmbRadius_Leave(null, null);
			string text = this.txtMaxSpace.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtMaxSpace.Text = this.txtMaxSpace.OldValue;
			}
			text = this.txtMaxSpace.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && text == "."))
			{
				this.txtMaxSpace.Text = "8";
			}
			text = this.txtLevel.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && (text == "-" || text == ".")) || (text.Length == 2 && (text == "-." || text == ".-")))
			{
				this.txtLevel.Text = this.txtLevel.OldValue;
			}
			text = this.txtLevel.Text.Trim();
			if (string.IsNullOrEmpty(text) || (text.Length == 1 && (text == "-" || text == ".")) || (text.Length == 2 && (text == "-." || text == ".-")))
			{
				this.txtLevel.Text = "3";
			}
		}

		private void FireRectLayoutForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveFormDataToXml();
		}

		protected override void WndProc(ref Message msg)
		{
			if (msg.Msg == 274 && (int)msg.WParam == 61536)
			{
				base.Hide();
				return;
			}
			base.WndProc(ref msg);
		}

		public double Radius
		{
			get
			{
				string text;
				if (string.IsNullOrEmpty(this.cmbRadius.Text.Trim()))
				{
					text = ((this.cmbRadius.Items.Count <= 0) ? "0" : this.cmbRadius.Items[0].ToString());
					return double.Parse(text);
				}
				text = this.cmbRadius.Text;
				if (!Assist.StringUtil.isNumberic2(text))
				{
					text = "0";
				}
				return Convert.ToDouble(text);
			}
		}

		public double MaxSpace
		{
			get
			{
				return Convert.ToDouble(this.txtMaxSpace.Text.Trim());
			}
		}

		public double Level
		{
			get
			{
				return Convert.ToDouble(this.txtLevel.Text.Trim());
			}
		}

		public int LinkLineWay
		{
			get
			{
				if (this.radioColLine.Checked)
				{
					return 2;
				}
				if (this.radioRowLine.Checked)
				{
					return 1;
				}
				return 0;
			}
		}

		public int WireStyle
		{
			get
			{
				return this.cmbWireStyle.SelectedIndex;
			}
		}

		public bool IsPreview
		{
			get
			{
				return this.checkPreview.Checked;
			}
		}

		private const string XmlNode_Root = "FireRect";

		private const string XmlNode_RoofSlope = "RoofSlope";

		private const string XmlNode_Radius = "Radius";

		private const string XmlNode_IsManual = "IsManual";

		private const string XmlNode_MaxSpace = "MaxSpace";

		private const string XmlNode_Level = "Level";

		private const string XmlNode_LinkWay = "LinkWay";

		private const string XmlNode_WireStyle = "WireStyle";

		private const string XmlNode_IsPreview = "IsPreview";

		private const string LocationX = "X";

		private const string LocationY = "Y";

		private Document doc;

		private FireDetectorType detectorType;
	}
}
